package com.ts.controller;

import com.ts.base.BaseController;
import com.ts.util.FileUtil;
import com.ts.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class lowerController extends BaseController{

    /**
     * 上位机 从 下位机获取文件
     */
//    @GetMapping("/download")
//    @ResponseBody
//    public void downloadFile(){
//        PageData pd = this.getPageData();
//        String sourceFileName = pd.getString("sourceFileName");
//        String targetPath = pd.getString("targetPath");
//        String ip = pd.getString("ip");
//        Integer port = Integer.valueOf(pd.get("port").toString());
//
//        String[] strings = FileUtil.fileFolder(targetPath);
//        String driver = strings[0].substring(0,1);
//        String filePath = "";
//        for(int i=1;i<strings.length;i++){
//            filePath += strings[i]+"+";
//        }
//
//        //下位机IP
//        //上位机接收文件：路径+文件名
//        //下位机文件所在位置
//        String upperURL = "http://" + ip + ":" + port+"/download/"+sourceFileName+"/"+driver+"/"+filePath;
//        try {
//            FileUtil.downLoad(targetPath,upperURL);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 上位机 给下位机传文件
     */
    @GetMapping("/upload")
    @ResponseBody
    public String uploadFile(){
        PageData pd = this.getPageData();
        String sourceFileName = pd.getString("sourceFileName");
        String targetPath = pd.getString("targetPath");
        String ip = pd.getString("ip");
        Integer port = Integer.valueOf(pd.get("port").toString());
        String[] strings = FileUtil.fileFolder(targetPath);
        String driver = strings[0].substring(0,1);
        String filePath = "";
        for(int i=1;i<strings.length;i++){
            filePath += strings[i]+"+";
        }
        String sourceFilepath = sourceFileName;
        String urlStr = "http://"+ip+":"+port+"/upload/"+driver+"/"+filePath.substring(0,filePath.length()-1);
        Map<String, String> fileMap = new HashMap<String, String>();
        //随便写入的参数
        fileMap.put("file", sourceFilepath);
        String ret = FileUtil.upload(urlStr, fileMap);
        return ret;
    }


    @RequestMapping("/upload/{driver}/{location}")
    @ResponseBody
    public Map<String, String> upload1(@RequestParam("file") MultipartFile file, @PathVariable("driver") String driver , @PathVariable("location") String location) throws IOException {
        log.info("[文件类型] - [{}]", file.getContentType());
        log.info("[文件名称] - [{}]", file.getOriginalFilename());
        log.info("[文件大小] - [{}]", file.getSize());

        String loc[] = location.split("\\+");
        String path = driver+":/";
        for(int i=0;i<loc.length;i++){
            path += loc[i]+"/";
        }
        File folder = new File(path);
        if(!folder.exists()){
            folder.mkdirs();
        }
        file.transferTo(new File(path+ file.getOriginalFilename()));
        Map<String, String> result = new HashMap<>(16);
        result.put("contentType", file.getContentType());
        result.put("fileName", file.getOriginalFilename());
        result.put("fileSize", file.getSize() + "");
        return result;
    }

}
